package com.geelaro.register.controller;

import com.geelaro.register.domain.entity.User;
import com.geelaro.register.domain.dto.UserDto;
import com.geelaro.register.service.impl.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.DateUtils;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

@Controller
public class UserController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());

	@Autowired
	private UserService userService;

	// 注册表单提交
	@PostMapping("/register")
	public String createUser(@ModelAttribute("user") @Valid UserDto userDto, BindingResult result, Model model) {
		if (result.hasErrors()) {
			return "register"; // 存在错误，返回注册界面
		}
		// name 是否已使用
		if (userService.checkUserByName(userDto.getName())) {
			result.rejectValue("name", "error.user", "用户名已被使用");
			logger.info("name已存在！");
			return "register";
		}
		// email 是否已注册。
		if (userService.checkUserByEmail(userDto.getEmail())) {
			result.rejectValue("email", "error.user", "该邮箱已被注册");
			logger.info("email已注册！");
			return "register";
		}
		// password 是否相等
		if (!checkPassWordUniform(userDto.getPassWd(), userDto.getMatchPassWd())) {
			result.rejectValue("passWd", "error.user", "两次输入密码不一致");
			return "register";
		}
		try {
			createUserAccount(userDto);
		} catch (DataIntegrityViolationException e) {
			result.rejectValue("email", "error.user", "Email already exists.");
			result.rejectValue("name", "error.user", "Name already exists");
		}
		return "login";// 返回登录界面
	}

	@GetMapping("/index")
	public String userInfo(Model model) {
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String name = userDetails.getUsername();

		//String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

		model.addAttribute("name", name);
		//model.addAttribute("date", date);
		return "index";
	}

	private User createUserAccount(UserDto userDto) {
		User registered = null;

		registered = userService.registerNewAccount(userDto);
		return registered;
	}

	private boolean checkPassWordUniform(String passWd, String matchPassWd) {
		return passWd.equals(matchPassWd);
	}

}
